package com.xls.commonlibrary.update;

import static com.xls.commonlibrary.wxapi.ConstWx.KEY_WX_UNION_ID;

import android.annotation.SuppressLint;
import android.webkit.ValueCallback;

import androidx.appcompat.app.AppCompatActivity;

import com.hjq.toast.Toaster;
import com.orhanobut.logger.Logger;
import com.xls.commonlibrary.BuildConfig;
import com.xls.commonlibrary.bean.UpdateVersionResult;
import com.xls.commonlibrary.http.Api;
import com.xls.commonlibrary.http.RetrofitHelper;
import com.xls.commonlibrary.http.RxLifecycle;
import com.xls.commonlibrary.http.Transformer;
import com.xls.commonlibrary.util.MMKVUtil;
import com.xls.commonlibrary.util.SysDataUtil;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Nonnull;

// 升级apk工具
public class CheckUpdateUtil {

    private static volatile CheckUpdateUtil singleton = null;

    private CheckUpdateUtil() {
    }

    public static CheckUpdateUtil getInstance() {
        if (singleton == null) {
            synchronized (CheckUpdateUtil.class) {
                if (singleton == null) {
                    singleton = new CheckUpdateUtil();
                }
            }
        }
        return singleton;
    }


    @SuppressLint("CheckResult")
    public void checkUpdate(
            @Nonnull AppCompatActivity activity,
            @Nonnull ValueCallback<UpdateVersionResult> callback
    ) {

        Map<String, String> p = new HashMap<>();
        p.put("appName", SysDataUtil.getInstance().getPkName());

        RetrofitHelper.getInstance().create(Api.class)
                .updateVersion(p)
                .compose(Transformer.transform())
                .compose(RxLifecycle.bindRxLifecycle(activity))
                .subscribe(
                        result -> {
                            if (result.getCode() == 1) {

                                if (callback != null) {
                                    callback.onReceiveValue(result.getData());
                                }

                            } else {

                                Toaster.show(result.getMsg());

                            }
                        },
                        throwable -> {

                        }
                );
    }

    // 登录第三步
    @SuppressLint("CheckResult")
    public void checkChannel(
            @Nonnull AppCompatActivity activity,
            @Nonnull ValueCallback<UpdateVersionResult> callback
    ) {

        Map<String, String> p = new HashMap<>();
        p.put("unionId", MMKVUtil.getInstance().getKV().decodeString(KEY_WX_UNION_ID));
        p.put("appName", SysDataUtil.getInstance().getPkName());
        p.put("apkName", BuildConfig.apkName);
        p.put("keyStore", BuildConfig.keyStore);

        RetrofitHelper.getInstance().create(Api.class)
                .yxqdDownload(p)
                .compose(Transformer.transform())
                .compose(RxLifecycle.bindRxLifecycle(activity))
                .subscribe(
                        result -> {
                            if (result.getCode() == 1) {

//                                Logger.d("====checkChannel=================%s", result.getData().toString());

                                if (callback != null) {
                                    callback.onReceiveValue(result.getData());
                                }

                            } else {
//                                -1 没有绑定关系,找不到上级
//                                -2 打包失败

                                Toaster.show(result.getMsg());

                            }
                        },
                        throwable -> {

                        }
                );
    }

}
